package com.study.springboottest.functionalThinking;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Test1 {

    /**
     * 假设我们有一个名字列表,其中一些条目由单个字符组成
     * 现在的任务是将去除单字符条目的列表内容,放在一个逗号分隔的字符串里返回
     * 且每个名字的首字母都要大写
     *
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("neal", "s", "stu", "bob", "j", "liam", "j");
        System.out.println(cleanNames(list));
        System.out.println(newCleanNames(list));
    }

    /**
     * 普通方式解决
     *
     * @param nameList
     * @return
     */
    public static String cleanNames(List<String> nameList) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < nameList.size(); i++) {
            if (nameList.get(i).length() > 1) {
                result.append(nameList.get(i).substring(0, 1).toUpperCase()).append(nameList.get(i).substring(1)).append(",");
            }
        }
        return result.substring(0, result.length() - 1);
    }

    /**
     * java8函数式编程解决
     *
     * @param nameList
     * @return
     */
    public static String newCleanNames(List<String> nameList) {
        if (nameList == null) {
            return "";
        }
        return nameList.stream()
                .filter(name -> name.length() > 1)
                .map(name -> name.substring(0, 1).toUpperCase() + name.substring(1))
                .collect(Collectors.joining(","));
    }
}
